import {$t} from '@/locales';
import dayjs from 'dayjs';

/**
 * Transform record to option
 *
 * @example
 *   ```ts
 *   const record = {
 *     key1: 'label1',
 *     key2: 'label2'
 *   };
 *   const options = transformRecordToOption(record);
 *   // [
 *   //   { value: 'key1', label: 'label1' },
 *   //   { value: 'key2', label: 'label2' }
 *   // ]
 *   ```;
 *
 * @param record
 */
export function transformRecordToOption<T extends Record<string, string>>(record: T) {
    return Object.entries(record).map(([value, label]) => ({
        value,
        label
    })) as CommonType.Option<keyof T>[];
}

/**
 * Translate options
 *
 * @param options
 */
export function translateOptions(options: CommonType.Option<string>[]) {
    return options.map(option => ({
        ...option,
        label: $t(option.label as App.I18n.I18nKey)
    }));
}

/**
 * Toggle html class
 *
 * @param className
 */
export function toggleHtmlClass(className: string) {
    function add() {
        document.documentElement.classList.add(className);
    }

    function remove() {
        document.documentElement.classList.remove(className);
    }

    return {
        add,
        remove
    };
}

const iconMap: any = {
    home: 'ant-design:dashboard-outlined',
    'device-manage': 'ant-design:database-outlined',
    'build-out-lined': 'ant-design:database-outlined',
    'to-one-out-lined': 'ant-design:alert-outlined',
    'system-manage': 'ant-design:setting-outlined',
    'user-manage': 'ant-design:user-outlined',
    'role-manage': 'ant-design:idcard-outlined',
    'organization-manage': 'ant-design:team-outlined',
    'project-manage': 'ant-design:project-outlined',
    'laboratory-manage': 'ic:outline-map',
    'collection-manage': 'carbon:ibm-cloud-transit-gateway',
    'lora-manage': 'fluent-mdl2:hard-drive',
}

export function handleOldMenuData(data: any) {
    const traverseTree = (node: any) => {
        if (node.title === '数据大屏') {
            node.meta = {
                title: '仪表盘面板',
                icon: iconMap[node.icon],
                hideInMenu: node.hidden,
                order: node.sort
            };
            node.component = 'layout.blank$view.dashboard';
            node.name = 'dashboard';
            delete node.children;
            delete node.redirect;
        } else {
            node.meta = {
                title: node.title,
                icon: iconMap[node.icon],
                hideInMenu: node.hidden,
                order: node.sort
            };
            if (node.parentId === 0 && node.alwaysShow === true) node.component = 'layout.base';
            if (node.parentId === 0 && node.alwaysShow === false) node.component = 'layout.blank';
            if (node.parentId !== 0) {
                node.name = `${node.component.replace(/\//g, '_').toLowerCase()}`;
                node.component = `view.${node.component.replace(/\//g, '_').toLowerCase()}`;
            }
            if (node.children && node.children.length > 0) {
                node.children.forEach((child: any) => traverseTree(child));
            }
        }
    };
    data.forEach((node: any) => traverseTree(node));
    return data;
}


export function startEndtime() {
    const today = dayjs().startOf('day');
    const startTime = today.valueOf();

    const endTime = dayjs().valueOf();

    return {startTime, endTime}
}
